package ports

import (
	"ddd-practice/domain"
	"time"
)

// product data transfer object
type ProductDTO struct {
	Id          int     `json:"id" comment:"唯一标识"`
	Name        string  `json:"name" comment:"名称"`
	Price       float32 `json:"price" comment:"价格"`
	Stock       int     `json:"stock" comment:"库存"`
	Description string  `json:"description" comment:"描述"`
	Image       string  `json:"image" comment:"商品图片"`

	CreateTime time.Time `comment:"创建时间"`
	UpdateTime time.Time `comment:"修改时间"`
}

// To data transfer object
func (p ProductDTO) ToDomainObject() (*domain.Product, error) {
	productDO, err := domain.NewProduct(
		p.Id,
		p.Name,
		p.Price,
		p.Stock,
		p.Description,
		p.Image,
		domain.Normal,
	)
	if err != nil {
		return nil, err
	}

	return productDO, nil
}

func (p *ProductDTO) GenerateFromDomainObject(product *domain.Product) {
	p.Id = product.Id()
	p.Name = product.Name()
	p.Price = product.Price()
	p.Description = product.Description()
	p.Image = product.Image()
	p.CreateTime = product.CreateTime()
	p.UpdateTime = product.UpdateTime()
}
